package com.android.pagemarket.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

import com.android.pagemarket.R;
import com.android.pagemarket.io.protocol.HttpManager;
import com.android.pagemarket.io.protocol.in.HttpAction;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.RegisterAction;
import com.android.pagemarket.io.protocol.out.HttpResult;
import com.android.pagemarket.util.Util;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-1
 * @see : 关注弹出框
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class FoucsPopWindow extends PopupWindow implements OnClickListener
{
    private EditText tx_name;
    private EditText tx_phone;
    Context mContext;
    
    public FoucsPopWindow(Context context)
    {
        super(LayoutInflater.from(context)
                .inflate(R.layout.popview_foucs, null),
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT );
        mContext = context;
        setBackgroundDrawable(new ColorDrawable(0x99000000));
        setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(true);
        
        initViews();
    }
    
    private void initViews()
    {
        View v = getContentView();
        v.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dismiss();
            }
        });
        tx_name = (EditText)v.findViewById(R.id.tx_name);
        tx_phone = (EditText)v.findViewById(R.id.tx_phone);
        v.findViewById(R.id.btn_register).setOnClickListener(this);
        v.findViewById(R.id.btn_see_later).setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_register:
            {
                doRegister();
                break;
            }
            case R.id.btn_see_later:
            {
                dismiss();
                break;
            }
        }
    }
    
    private void doRegister()
    {
        String name = tx_name.getEditableText().toString().trim();
        String phone = tx_phone.getEditableText().toString().trim();
        
        if(name.length() == 0 || !Util.isValidName(name))
        {
            Util.showToast(R.string.tip_name_check);
            return;
        }
        if(phone.length() == 0 || !Util.isPhoneNumberValid(phone))
        {
            Util.showToast(R.string.tip_phone_check);
            return;
        }
        
        dismiss();
        
        RegisterAction action = new RegisterAction(name, phone);
        action.setActionListener(new ActionListener<HttpResult>()
        {
            @Override
            public void onSucceed(HttpResult result)
            {
                if(result.isNoError())
                {
                    Util.showToast(result.resultInfo);
                    dismiss();
                }else
                {
                    Util.showToast(result.resultInfo);
                }                
            }
            @Override
            public void onError(int resultCode)
            {
                Util.showToast(HttpAction.getErrorMean(resultCode));
            }
        });
        HttpManager.getInstance().submitAction(action);
    }
}
